// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ClientSocket.h"
#include "ClientDlg.h"
#include "Defines.h"


// ClientSocket

byte* buff_tmp;
int offset;

ClientSocket::ClientSocket()
{
	m_clientID = -1;
	pDlg = NULL;
	buff_tmp = NULL;
	offset = 0;
}

ClientSocket::~ClientSocket()
{
}


// ClientSocket member functions

void ClientSocket::OnReceive(int nErrorCode)
{
	OutputDebugString(_T("[Client Socket]: OnReceive"));
	char* buff = ((CClientDlg*)pDlg)->m_szBuff;
	int rSize = Receive(buff, BUFF_SIZE);
	if(rSize < BUFF_SIZE)
	{
		OutputDebugString(_T("[Client Socket]: 12121212121212121212221"));
		if(buff_tmp == NULL)
		{
			buff_tmp = new byte[BUFF_SIZE];
			offset = 0;
		}
		memcpy(buff_tmp + offset, buff, rSize);
		offset += rSize;
		if(offset >= BUFF_SIZE)
		{
			OutputDebugString(_T("[Client Socket]: 99999999999999999999999999999"));
			memcpy(buff, buff_tmp, BUFF_SIZE);
			((CClientDlg*)pDlg)->OnReceive(buff, BUFF_SIZE);
			delete buff_tmp;
			buff_tmp = NULL;
		}
	}
	else
	{
		((CClientDlg*)pDlg)->OnReceive(buff, BUFF_SIZE);
	}
	//((CClientDlg*)pDlg)->OnReceive(buff, BUFF_SIZE);

	/*
	char buff[1000];
	Receive(buff, 1000);

	if(m_clientID < 0)
	{
		m_clientID = atoi(buff);
		CString str;
		str.Format(_T("Client: %d"), m_clientID);
		((CClientDlg*)pDlg)->SetWindowText(str);
	}
	else
	{
		((CClientDlg*)pDlg)->OnReceive(buff, 1000);
	}
	*/

	CSocket::OnReceive(nErrorCode);
}

void ClientSocket::OnSend(int nErrorCode)
{
	

	CSocket::OnSend(nErrorCode);
}

void ClientSocket::OnClose(int nErrorCode)
{
	
	m_clientID = -1;
	CSocket::OnClose(nErrorCode);
}
